package com.jy.adscm.operation.newhscrb.controller;


import com.baomidou.mybatisplus.plugins.Page;
import com.jy.adscm.exception.AdscmSystemException;
import com.jy.adscm.operation.newhscrb.bean.NhcProcedure;
import com.jy.adscm.operation.newhscrb.bean.QueryNhcProcedure;
import com.jy.adscm.operation.newhscrb.service.NhcProcedureService;
import com.jy.adscm.security.utils.Token;
import com.jy.adscm.support.SimpleCoreResponse;
import com.jy.adscm.utils.AdscmBaseUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * <p>
 * 耗材工序 前端控制器
 * </p>
 *
 * @author zyl
 * @since 2020-06-23
 */
@RestController
@RequestMapping("/adscm/operation/nhcProcedure")
public class NhcProcedureController {

    @Autowired
    RedisTemplate<Object, Token> tokenRedisTemplate;
    @Autowired
    NhcProcedureService nhcProcedureService;
    @RequestMapping("insert")
    public SimpleCoreResponse insert(@RequestBody NhcProcedure nhcProcedure, HttpServletRequest request) throws AdscmSystemException {
        String tokenId = request.getParameter("tokenId");
        Token token = AdscmBaseUtils.getInstance().getTokenByTokenId(tokenId, tokenRedisTemplate);
        nhcProcedure.setCreator(token.getUser().getId().toString());
        nhcProcedure.setCreateTime(new Date());
        boolean insert = nhcProcedureService.insertNhcProcedure(nhcProcedure);
        return new SimpleCoreResponse("200","新增成功",insert);
    }
    @RequestMapping("update")
    public SimpleCoreResponse update(@RequestBody NhcProcedure nhcProcedure, HttpServletRequest request) throws AdscmSystemException {
        String tokenId = request.getParameter("tokenId");
        Token token = AdscmBaseUtils.getInstance().getTokenByTokenId(tokenId, tokenRedisTemplate);
        nhcProcedure.setCreator(token.getUser().getId().toString());
        nhcProcedure.setCreateTime(new Date());
        boolean update = nhcProcedureService.updateNhcProcedure(nhcProcedure);
        return new SimpleCoreResponse("200","修改成功",update);
    }
    @RequestMapping("select")
    public SimpleCoreResponse select(@RequestBody QueryNhcProcedure queryNhcProcedure, HttpServletRequest request) throws AdscmSystemException {
        String tokenId = request.getParameter("tokenId");
        Token token = AdscmBaseUtils.getInstance().getTokenByTokenId(tokenId, tokenRedisTemplate);
        Page<NhcProcedure> page=nhcProcedureService.select(queryNhcProcedure);
        return new SimpleCoreResponse("200","查询成功",page);
    }
}

